/*
 * @Descripttion: 
 * @Author: voanit
 * @Date: 2022-06-07 18:06:00
 * @LastEditors: voanit
 * @LastEditTime: 2022-06-08 12:25:04
 */
import { ArticleAction } from '@/types/store'
import { Article } from '@/types/data'
// import { getToken } from '@/utils/storage'
type ArticleState = {
  [key: number]: {
    timestamp: string
    articles: Article[]
  }

}
const initialState: ArticleState = {

}
const article = (state = initialState, action: ArticleAction) => {
  switch (action.type) {
    case 'article/saveChannelArticles':
      const { channel_id, timestamp, articles } = action.payload
      const old = state[channel_id]?.articles || []
      return {
        ...state,// 需要追加数据
        [channel_id]: {
          timestamp,
          articles: [...old, ...articles],
        }
      }
    case 'article/getNewArticles':
      // const { channel_id, timestamp, articles } = action.payload
      // const old = state.channelArticles[channel_id]?.articles || []
      // console.log(action.payload, 72);

      return {
        // 需要追加数据
        [action.payload.channel_id]: {
          timestamp: action.payload.timestamp,
          articles: [...action.payload.articles],
        }
      }
    default:
      return state
  }
}

export default article